apply plugin: 'java'
apply plugin: 'distribution'

sourceCompatibility = 1.6
version = '0.0.2'

/*
This script depends on the layout of depots. It assumes a file structure of
some directory
    couchbase-list-java-native
    ...
    thali

In other words that there is a single directory that contains all the relevant depots below including Thali.
 */

task buildCouchbaseLiteJavaNative(type: GradleBuild) {
    buildFile = '../../couchbase-lite-java-native/build.gradle'
    tasks = ['test', 'uploadArchives']
}

task buildCouchbaseLiteJavaCore(type: GradleBuild, dependsOn: 'buildCouchbaseLiteJavaNative') {
    buildFile = '../../couchbase-lite-java-core/build.gradle'
    tasks = ['test', 'uploadArchives']
}

task buildCouchbaseLiteJavaListener(type: GradleBuild, dependsOn: 'buildCouchbaseLiteJavaCore') {
    buildFile = '../../couchbase-lite-java-listener/build.gradle'
    tasks = ['test', 'uploadArchives']
}

// The test task is not included because of https://github.com/couchbase/couchbase-lite-java/issues/15
task buildCouchbaseLiteJava(type: GradleBuild, dependsOn:'buildCouchbaseLiteJavaListener') {
    buildFile = '../../couchbase-lite-java/build.gradle'
    tasks = ['uploadArchives']
}

// I had to remove the connectedAndroidTest task because a bunch of the tests fail
// https://github.com/couchbase/couchbase-lite-android/issues/287#issuecomment-41827162
task buildCouchbaseLiteAndroid(type: GradleBuild, dependsOn: 'buildCouchbaseLiteJava') {
    buildFile = '../../couchbase-lite-android/build.gradle'
    tasks = ['uploadArchives']
}

// NOTE: I AM NOT BUILDING EKTORP! We haven't edited it in forever, it uses maven to build and not gradle and I
// need to check if they finally released the version with our changes so we can stop thinking about it all together (e.g.
// stop using the snapshot)

task buildThaliUniversalUtilities(type: GradleBuild, dependsOn: 'buildCouchbaseLiteAndroid') {
    buildFile = 'Utilities/UniversalUtilities/build.gradle'
    tasks = ['test', 'uploadArchives']
}

task buildThaliJavaUtilities(type: GradleBuild, dependsOn: 'buildThaliUniversalUtilities') {
    buildFile = 'Utilities/JavaUtilities/build.gradle'
    tasks = ['test', 'uploadArchives']
}

task buildThaliAndroidUtilities(type: GradleBuild, dependsOn: 'buildThaliJavaUtilities') {
    buildFile = 'Utilities/AndroidUtilities/build.gradle'
    tasks = ['connectedAndroidTest', 'uploadArchives']
}

task buildThaliDeviceHubUniversal(type: GradleBuild, dependsOn: 'buildThaliAndroidUtilities') {
    buildFile = 'ThaliDeviceHub/Universal/build.gradle'
    tasks = ['test', 'uploadArchives']
}

task buildThaliDeviceHubJava(type: GradleBuild, dependsOn: 'buildThaliDeviceHubUniversal') {
    buildFile = 'ThaliDeviceHub/java/build.gradle'
    tasks = ['test', 'distZip'] // This isn't a library, it's an 'executable', this builds the distribution
}

task buildThaliDeviceHubAndroid(type: GradleBuild, dependsOn: 'buildThaliDeviceHubJava') {
    buildFile = 'ThaliDeviceHub/android/android/build.gradle'
    tasks = ['connectedAndroidTest', 'build'] // This isn't a library, it's an 'executable', this builds the apk file
}

task buildThaliAndroidSdk(type: GradleBuild, dependsOn:'buildThaliDeviceHubAndroid') {
    buildFile = 'ThaliApplicationSDKs/AndroidPouchDbSDK/build.gradle'
    tasks = ['connectedAndroidTest', 'build'] // This isn't a library, it's a wrapper project that produces an executable
}

task globalBuild(dependsOn: 'buildThaliAndroidSdk'){}


// BUILD THE PPNET DEMO

distributions {
    main {
        baseName = 'ThaliPPNETDemo'
        contents {
            from { 'build/PPNET'}
        }
    }
}

task deletePPNETBuildDirectory(type: Delete) {
    delete 'build/PPNET'
}

task copyChromeNativeMessagingHost(type: Copy, dependsOn: deletePPNETBuildDirectory) {
    from 'Utilities/DotNetUtilities/DotNetUtilities/ChromeNativeMessagingHost/bin/debug/'
    into 'build/PPNET/ChromeNativeMessagingHost'
}

task copyChromeExtension(type:Copy, dependsOn: copyChromeNativeMessagingHost) {
    from 'Utilities/DotNetUtilities/DotNetUtilities/ChromeExtension'
    into 'build/PPNET/ChromeExtension'
}

task deleteIdeaFolderFromChromeExtension(type: Delete, dependsOn: copyChromeExtension) {
    delete 'build/PPNET/ChromeExtension/.idea'
}

task copyAndroidTDH(type: Copy, dependsOn: deleteIdeaFolderFromChromeExtension) {
    from 'ThaliDeviceHub/android/android/build/apk/android-debug-unaligned.apk'
    into 'build/PPNET/ThaliDeviceHubs'
}

// Adapted from http://mrhaki.blogspot.com/2012/06/gradle-goodness-unpacking-archive.html
task unzipAndCopyJavaTDH(type: Copy, dependsOn: copyAndroidTDH) {
    from zipTree(file('ThaliDeviceHub/java/build/distributions/java.zip'))
    into 'build/PPNET/ThaliDeviceHubs/'
}

task copyPPNETFilesIntoChromeExtension(type: Copy, dependsOn: unzipAndCopyJavaTDH) {
    from '../../ppnet/'
    into 'build/PPNET/ChromeExtension/apps'
}

task copyPouchAPKToOwnDirectory(type: Copy, dependsOn: copyPPNETFilesIntoChromeExtension) {
    from 'build/PPNET/ChromeExtension/apps/AndroidPouchDbSDK-debug-unaligned.apk'
    into 'build/PPNET/AndroidClient/'
}

task deletePouchAPKFromChromeExtension(type: Delete, dependsOn: copyPouchAPKToOwnDirectory) {
    delete 'build/PPNET/ChromeExtension/apps/AndroidPouchDbSDK-debug-unaligned.apk'
}

distZip.dependsOn deletePouchAPKFromChromeExtension

task buildPPNETDemo(dependsOn: distZip) {
}



